<?php
/* ******************************
 *	transactions aJax actions
 * ******************************
 */
 
require_once '../../includes/classes/core.php';

$action 	= $_REQUEST['act'];
$user_id	= $_COOKIE['USERID'];
$error 		= '';
$data 		= '';
	
switch ($action) {
	case 'get_edit_page':
		$sale_detail_id 		= $_REQUEST['id'];
		
		$data		= array('page' => Get_count_change($sale_detail_id));
		break;

	case 'get_list':
		$trans_id = $_REQUEST['tarns_hid_id'];
	    $count = $_REQUEST['count'];
	    $hidden = $_REQUEST['hidden'];
	    
        $rResult = mysql_query("SELECT 	  sale_transaction_detail.id,
										  sale_transaction.start_date,
        								  production.code,
        								  production.name,
        								  sale_transaction_detail.product_quantity,
										  sale_transaction_detail.cost,
										  sale_transaction_status.description
								FROM 	  sale_transaction_detail
								LEFT JOIN sale_transaction ON sale_transaction.id=sale_transaction_detail.transaction_id
								LEFT JOIN sale_transaction_status ON sale_transaction_status.id=sale_transaction.`status`
        						LEFT JOIN production ON sale_transaction_detail.production_id=production.`id`
								WHERE 	  sale_transaction_detail.transaction_id=$trans_id AND sale_transaction_detail.status=1");
		
		$data = array(
			"aaData"	=> array()
		);
		
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$data['aaData'][] = $row;
		}
		
        break;
    case 'disable':
    	
		$trans_id = $_REQUEST['id'];
		
		$transaction_id = transaction($trans_id);
		
		$quantity   	= GettransQuantity($trans_id);
		
		$prod_id   		= production_id($trans_id);
		
		$price      	= prod_price($trans_id);
		
		$sum_price  	= $quantity*$price;
		
		
		DisableProduct($trans_id);
		
		update_transact_sumprice($sum_price, $transaction_id);
		
		SaleunDone($prod_id, $quantity);
		
		 break;
	 case 'count_change':
	 	$detail_id     	 = $_REQUEST['detail_id'];
	 	$old_quantity  	 = $_REQUEST['old_quantity'];
	 	$new_quantity  	 = $_REQUEST['new_quantity'];
	 	$production_id 	 = $_REQUEST['production_id'];
	 	$cost 	 		 = $_REQUEST['cost'];
	 	$transaction_id  = $_REQUEST['transaction_id'];
	 	
	 	$corect_quantity = $old_quantity - $new_quantity;
	 	
	 	$sum_price       = $corect_quantity*$cost;
	 	
	 	update_transact_count($new_quantity, $detail_id);
	 	
	 	update_transact_sumprice($sum_price, $transaction_id);
	 	
	 	SaleunDone($production_id, $corect_quantity);
	 	
	 	break;
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	transactions Functions
 * ******************************
 */

function GettransQuantity($trans_id){
	
	$res = mysql_query("SELECT 	sale_transaction_detail.product_quantity
						FROM 	sale_transaction_detail
						WHERE 	sale_transaction_detail.id=$trans_id");
	
	$row = mysql_fetch_assoc($res);
	
	return $row['product_quantity'];
}


function transaction($trans_id){
	
	$res = mysql_query("SELECT 	sale_transaction_detail.transaction_id
						FROM 	sale_transaction_detail
						WHERE 	sale_transaction_detail.id=$trans_id");
	
	$row = mysql_fetch_assoc($res);
	
	return $row['transaction_id'];
}


function production_id($trans_id){
	
	$res = mysql_query("SELECT sale_transaction_detail.`production_id`
		    			FROM   sale_transaction_detail
		    			WHERE  sale_transaction_detail.id=$trans_id");
	
			$row = mysql_fetch_assoc($res);
			
			return $row['production_id'];
}

function prod_price($trans_id){
	
	$res = mysql_query("SELECT sale_transaction_detail.`cost`
						FROM   sale_transaction_detail
						WHERE  sale_transaction_detail.id=$trans_id");
	
	$row = mysql_fetch_assoc($res);
	
	return $row['cost'];
}

function update_transact_sumprice($sum_price, $transaction_id){
	
	mysql_query("UPDATE sale_transaction
 				 SET    sale_transaction.`payed_money` = sale_transaction.`payed_money` - $sum_price
 			     WHERE  sale_transaction.id  = $transaction_id");
	
}

function update_transact_count($new_quantity, $detail_id){
	
	mysql_query("UPDATE sale_transaction_detail
				 SET    sale_transaction_detail.`product_quantity` = $new_quantity
				 WHERE  sale_transaction_detail.id  = $detail_id");

}

function SaleunDone($prod_id, $quantity){
	
$req_quantity = $quantity;

$res = mysql_query("SELECT   `reserve`.id,
							 `reserve`.`quantity`,
							 `reserve`.`sold`
					FROM 	 `reserve`
					WHERE 	 `reserve`.`production_id` = $prod_id
					ORDER BY `reserve`.`id` desc" );

	while ($row = mysql_fetch_array($res)) {
		
		if ($req_quantity != 0 && $row['sold']!=0) {
			
			if($row['sold'] >= $req_quantity){
				
				$sum = $row['sold'] - $req_quantity;
				mysql_query("	UPDATE	`reserve`
								SET		`sold` = $sum
								WHERE	`id`   = $row[id]");
				$req_quantity = 0;
			}else{
				
				$sum = 0;
				mysql_query("	UPDATE	`reserve`
								SET		`sold` = $sum
								WHERE	`id`   = $row[id]" );
				$req_quantity = $req_quantity - $row['sold'];
			}
		}
	}
}

function DisableProduct($trans_id){
	
		$transaction_id =transaction($trans_id);
					
		mysql_query("UPDATE sale_transaction_detail
					 SET    sale_transaction_detail.`status` = 3
					 WHERE  sale_transaction_detail.id       = $trans_id");
				    
		$res = mysql_query("SELECT sale_transaction_detail.id
			    			FROM   sale_transaction_detail
			    			WHERE  sale_transaction_detail.transaction_id=$transaction_id 
							AND sale_transaction_detail.status=1");
	    $check = mysql_num_rows($res);
	    	
		if($check==0){
		 	mysql_query("UPDATE sale_transaction
		 			     SET    sale_transaction.`status` = 3
		 				 WHERE  sale_transaction.id       = $transaction_id");
		 }
}
function Get_count_change($sale_id){

$res = mysql_fetch_array(mysql_query("	SELECT  sale_transaction_detail.id as detail_id,
												sale_transaction_detail.cost as cost,
												sale_transaction_detail.transaction_id as transaction_id,
												sale_transaction_detail.`production_id` as production_id,
												sale_transaction_detail.product_quantity AS maximum,
												production.`name` AS prod_name,
												'1' AS minimum
													
												FROM  `sale_transaction_detail`
												LEFT JOIN   pricelist ON sale_transaction_detail.production_id=pricelist.production_id
												LEFT JOIN   production ON production.id=sale_transaction_detail.production_id
												WHERE  sale_transaction_detail.id = $sale_id"));
	$maxim = $res['maximum'];
	$minim = $res['minimum'];

	$data = "";
	$data.='<div>
			   <fieldset>
					<table class="dialog-form-table">
						<tr>
							<td style="width: 170px;"><label for="money-amount">მიუთითეთ რაოდენობა:</label></td>
							<td><input style="width: 40px; height: 26px;" type="number" name="quantity" step="1" min='.$minim.' max='.$maxim.' id="prod_new_count" class="idle" onblur="this.className=\'idle\'" value="'.$maxim.'"/></td>
						</tr>
					</table>
		        </fieldset>
					<input id="detail_id" type="text" value="'. $res['detail_id'] .'" style="display: none;">
	 				<input id="product_quantity" type="text" value="'. $res['maximum'] .'" style="display: none;">
	 				<input id="production_id" type="text" value="'. $res['production_id'] .'" style="display: none;">
	 				<input id="cost" type="text" value="'. $res['cost'] .'" style="display: none;">
	 				<input id="transaction_id" type="text" value="'. $res['transaction_id'] .'" style="display: none;">
	 		</div>';

	return $data;
}
?>